import bean.Jsonbean;
import bean.Loginbean;
import com.google.gson.Gson;
import dao.LoginDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "RsServlet")
public class RsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out= response.getWriter();
        String name = request.getParameter("zuser");
        String pwd = request.getParameter("zpwd");
        Loginbean loginbean = new Loginbean(name,pwd);
        if(name==""||pwd==""){
            Jsonbean jsonbean2 = new Jsonbean(2,"他传过来的是个空的字符串","用户名或密码不能为空");
            Gson gson = new Gson();
            String json2 = gson.toJson(jsonbean2);
            out.println(json2);
        }else {
            Jsonbean jsonbean = new Jsonbean(1,loginbean,"注册成功");
            Jsonbean jsonbean1 = new Jsonbean(0,"null","用户名已存在，请重新注册");

            Gson gson = new Gson();
            String json = gson.toJson(jsonbean);
            String json1 = gson.toJson(jsonbean1);
            LoginDao loginDao = new LoginDao();
            if (loginDao.YanZ(loginbean)){
                loginDao.Register(loginbean);
                out.println(json);
                System.out.println("注册成功");
            }
            else {
                out.print(json1);
            }

        }

        }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }
}

